<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% @context ||= @current_user %>
<% cache(['wiki_sidebar_render_3', @context.cache_key, @current_user, @context.feature_enabled?(:usage_rights_required)].cache_key.to_s) do %>
<% load_wiki_sidebar %>
<% show_files_tab = @wiki_sidebar_data[:root_folders].any?{|rf| rf.grants_right?(@current_user, session, :read_contents)} %>
<div id="editor_tabs" class="ui-tabs--mini" style="display: none;">

  <span class="wiki-sidebar-header" role="heading" aria-level="2">{{#t}}Insert Content into the Page{{/t}}</span>
  <ul>
    <li><a href="#editor_tabs_2">{{#t}}Links{{/t}}</a></li>
    <% if show_files_tab %>
      <li><a href="#editor_tabs_3">{{#t}}Files{{/t}}</a></li>
    <% end %>
    <li><a href="#editor_tabs_4">{{#t}}Images{{/t}}</a></li>
  </ul>
  <div id="editor_tabs_2" role="application">
    <%= @context.is_a?(Group) ? t('group_links', 'Link to other content in the group.') : t('course_links', 'Link to other content in the course.') %>
    <% if @context.is_a?(Course) || @context.is_a?(Group) %>
      <a href="<%= context_url(@context, :retrieve_context_external_tools_url, :url => "{{ url }}") %>" id="external_tool_retrieve_url" style="display: none;">&nbsp;</a>
    <% end %>
    <% if @context.is_a?(Course) %>
      <a href="<%= context_url(@context, :context_external_tool_resource_selection_url, "{{ id }}", :editor => '1') %>" id="context_external_tool_resource_selection_url" style="display: none;">&nbsp;</a>
    <% end %>
    {{#t}}Click any page to insert a link to that page.{{/t}}
    <div id="pages_accordion" class="ui-accordion--mini" role="tabpanel">
      <% if @context.respond_to?(:wiki) %>
      <a href="<%= polymorphic_url([@context, :wiki_page], id: '{{ page_url }}') %>" id="wiki_sidebar_wiki_url" style="display: none;">&nbsp;</a>
      <div>
        <div class="header" aria-controls="pages_tab_panel" role="tab"><a href="#">{{#t}}Pages{{/t}}</a></div>
        <div id="pages_tab_panel" role="tabpanel">
            <ul class="wiki_pages page_list">
              <% (@wiki_sidebar_data[:wiki_pages]).each do |wiki_page|  %>
                <% if can_do(@wiki_sidebar_data[:wiki], @current_user, :manage) || wiki_page.published? %>
                  <li title="{{#t}}Click to insert a link to this page{{/t}}"><%= link_to wiki_page.title, polymorphic_path([@context, wiki_page]), :role => "button"%></li>
                <% end %>
              <% end %>
            </ul>
            <% if can_do(@wiki_sidebar_data[:wiki], @current_user, :create_page) %>
              <%= link_to t('links.new_page', "Link to a New Page"), "#", :id => 'new_page_link', :class => "icon-add" %>
              <form id="new_page_drop_down" style="display:none; margin: 5px;">
                <div class="row-fluid">
                  <label for="new_page_name">{{#t}}What would you like to call the new page?{{/t}}</label><br />
                  <input type="text" name="new_page_name" id="new_page_name" class="span12">
                  <input type="hidden" name="new_page_url_prefix" value="<%= polymorphic_path([@context, :wiki_pages]) %>" id="new_page_url_prefix" />
                  <button id="new_page_submit" class="btn btn-small" type="submit">{{#t}}Insert Link{{/t}}</button>
                </div>
              </form>
            <% end %>
        </div>
      </div>
      <% end %>
      <% if @context.respond_to?(:assignments) %>
      <div>
        <div aria-controls="assignment_panel" role="tab" class="header" aria-controls="wiki_pages_panel"><a href="#">{{#t}}Assignments{{/t}}</a></div>
         <div id="assignment_panel" role="tabpanel">
            <ul class="wiki_pages page_list">
              <li title="{{#t}}Click to insert a link to this assignment{{/t}}"><%= link_to t('links.assignments', "Assignments"), context_url(@context, :context_assignments_url)%></li>
              <% for assignment in @wiki_sidebar_data[:active_assignments].select{|a| !a.new_record?} %>
                <li title="{{#t}}Click to insert a link to this assignment{{/t}}"><%= link_to assignment.title, context_url(@context, :context_assignment_url, assignment) %></li>
              <% end %>
            </ul>
         </div>
      </div>
      <% end %>
      <% if @context.respond_to?(:quizzes) %>
      <div>
        <div aria-controls="quizzes_panel" class="header" role="tab"><a href="#">{{#t}}Quizzes{{/t}}</a></div>
         <div id="quizzes_panel" role="tabpanel">
            <ul class="wiki_pages page_list">
              <li title="{{#t}}Click to insert a link to this quiz{{/t}}"><%= link_to t('links.quizzes', "Quizzes"), context_url(@context, :context_quizzes_url) %></li>
              <% for quiz in @wiki_sidebar_data[:active_quizzes].select{|a| !a.new_record?} %>
                <li title="{{#t}}Click to insert a link to this quiz{{/t}}"><%= link_to quiz.title, context_url(@context, :context_quiz_url, quiz) %></li>
              <% end %>
            </ul>
         </div>
      </div>
      <% end %>
      <% if @context.respond_to?(:announcements) %>
      <div>
        <div class="header" aria-controls="announcements_panel" role="tab"><a href="#">{{#t}}Announcements{{/t}}</a></div>
         <div id="announcements_panel" role="tabpanel">
            <ul class="wiki_pages page_list">
              <li title="{{#t}}Click to insert a link to this announcement{{/t}}"><%= link_to t('links.announcements', "Announcements"), context_url(@context, :context_announcements_url) %></li>
              <% for announcement in @wiki_sidebar_data[:active_discussion_topics].select{ |d| d.is_a?(Announcement) } %>
                <li title="{{#t}}Click to insert a link to this announcement{{/t}}"><%= link_to announcement.title, context_url(@context, :context_announcement_url, announcement) %></li>
              <% end %>
            </ul>
         </div>
      </div>
      <% end %>

      <% if @context.respond_to?(:discussion_topics) %>
      <div>
        <div role="tab" aria-controls="discussions_panel" class="header"><a href="#">{{#t}}Discussions{{/t}}</a></div>
         <div id="discussions_panel" role="tabpanel">
            <ul class="wiki_pages page_list">
              <li title="{{#t}}Click to insert a link to this topic{{/t}}"><%= link_to t('links.discussions', "Discussions"), context_url(@context, :context_discussion_topics_url) %></li>
              <% for discussion_topic in @wiki_sidebar_data[:active_discussion_topics].reject{ |d| d.is_a?(Announcement) } %>
                <li title="{{#t}}Click to insert a link to this topic{{/t}}"><%= link_to discussion_topic.title, context_url(@context, :context_discussion_topic_url, discussion_topic) %></li>
              <% end %>
            </ul>
         </div>
      </div>
      <% end %>

      <% if @context.respond_to?(:context_modules) %>
      <div>
        <div role="tab" aria-controls="modules_panel" class="header"><a href="#">{{#t}}Modules{{/t}}</a></div>
          <div id="modules_panel" role="tabpanel">
            <ul class="wiki_pages page_list">
              <li title="{{#t}}Click to insert a link to this module{{/t}}"><%= link_to t('links.modules', "Modules"), context_url(@context, :context_context_modules_url) %></li>
              <% for m in @wiki_sidebar_data[:active_context_modules] %>
                <li title="{{#t}}Click to insert a link to this module{{/t}}"><%= link_to m.name, context_url(@context, :context_context_module_url, m) %></li>
              <% end %>
            </ul>
          </div>
      </div>
      <% end %>

      <div>
        <div class="header" role="tab" aria-controls="groups_panel"><a href="#"><%= @context.is_a?(Group) ? t('links_to.nav.group', 'Group Navigation') : t('links_to.nav.course', 'Course Navigation') %></a></div>
         <div role="tabpanel" id="groups_panel">
            <ul class="wiki_pages page_list">
              <% if @context.respond_to?(:assignments) %>
                <li title="{{#t}}Click to insert a link to the assignments page{{/t}}"><%= link_to t('links.assignments_page', "Assignments"), context_url(@context, :context_assignments_url)%></li>
              <% end %>
              <% if @context.respond_to?(:wiki) %>
                <li title="{{#t}}Click to insert a link to the wiki{{/t}}"><%= link_to t('links.wiki_pages_index', "Pages"), polymorphic_url([@context, :wiki_pages]) %></li>
              <% end %>
              <% if @context.respond_to?(:discussion_topics) %>
                <li title="{{#t}}Click to insert a link to the discussions page{{/t}}"><%= link_to t('links.discussions_page', "Discussions"), context_url(@context, :context_discussion_topics_url)%></li>
              <% end %>
              <% if @context.respond_to?(:assignments) %>
                <li title="{{#t}}Click to insert a link to the syllabus{{/t}}"><%= link_to t('links.syllabus', "Syllabus"), context_url(@context, :syllabus_context_assignments_url)%></li>
              <% end %>
              <% if @context.respond_to?(:announcements) %>
                <li title="{{#t}}Click to insert a link to the announcements page{{/t}}"><%= link_to t('links.announcements_page', "Announcements"), context_url(@context, :context_announcements_url)%></li>
              <% end %>
              <% if @context.respond_to?(:attachments) %>
                <li title="{{#t}}Click to insert a link to the files page{{/t}}"><%= link_to t('links.files_page', "Files"), context_url(@context, :context_files_url)%></li>
              <% end %>
              <% collaborations_url = context_url(@context, :context_collaborations_url) rescue nil %>
              <% if collaborations_url %>
                <li title="{{#t}}Click to insert a link to the collaborations page{{/t}}"><%= link_to t('links.collaborations_page', "Collaborations"), collaborations_url%></li>
              <% end %>
              <% if @context.is_a?(Course) %>
                <li title="{{#t}}Click to insert a link to the grades page{{/t}}"><%= link_to t('links.grades_page', "Grades"), context_url(@context, :context_grades_url)%></li>
              <% end %>
              <% if @context.respond_to?(:users) %>
                <li title="{{#t}}Click to insert a link to the people page{{/t}}"><%= link_to t('links.users_page', "People"), context_url(@context, :context_users_url)%></li>
              <% end %>
              <% if @context.respond_to?(:context_modules) %>
                <li title="{{#t}}Click to insert a link to the modules page{{/t}}"><%= link_to t('links.modules_page', "Modules"), context_url(@context, :context_context_modules_url)%></li>
              <% end %>
            </ul>
         </div>
      </div>
    </div>

  </div>
  <% if show_files_tab %>
  <div id="editor_tabs_3">
    {{#t}}Click any file to insert a download link for that file.{{/t}}
    <div id="wiki_sidebar_file_uploads" class="uploading_file_list" style="display: none;">
      <div class="uploading">
        {{#t}}uploading_file', %{Uploading *file*...}, :wrapper => '<span class="filename">\1</span>{{/t}}
        <div>
          <%= image_tag "ajax-loader-bar.gif", :alt => t('images.alt.loading', "Loading...") %>
        </div>
      </div>
      <div class="remaining">
        <%= t 'files_remaining', '*0* more to upload', :wrapper => '<span class="files_remaining">\1</span>' %>
      </div>
    </div>
    <div class="file_list_holder <%= 'file_drop' if can_do(@context, @current_user, :manage_files) %>">
      <div class="dragging_message">{{#t}}Drop Files Here to upload{{/t}}</div>
      <div id="file_list_container" class="file_list" style="max-height: 300px; overflow: auto;">
        <a id="folder_url" href="<%= context_url(@context, :context_folder_url, '{{ id }}').html_safe %>" style="display: none;">&nbsp;</a>
        <ul id="tree1" role="tree" data-level="0" tabindex="0" aria-label="{{#t}}Files, Use the up and down arrow keys to navigate through the tree. Press right to expand folders, left to collapse {{/t}}">
          <% @wiki_sidebar_data[:root_folders].each do |folder| %>
            <li aria-level="0" aria-selected="false" role="treeitem" data-id="<%= folder.id %>" class="folder folder_<%= folder.id %>" id="root_folder_<%= folder.id %>">
            
            <span class='name'><%= folder.name %> </span>
              <ul role="group"></ul>
            </li>
          <% end %>
          <li aria-selected="false" role="treeitem" class="file file_blank" style="display: none;">
            <span class='name' rel="<%= context_url(@context, :context_url) %>/files/{{ id }}/download?wrap=1"></span>
          </li>
          <li aria-selected="false" role="treeitem" class="folder_blank" style="display: none;">
            <span class='name'></span>
            <ul role="group"></ul>
          </li>
          <li class="loading" style="display: none;">
            <%= t 'messages.loading', "Loading..." %>
          </li>
        </ul>
      </div>
    </div>
    <% if can_do(@context, @current_user, :manage_files) %>
      <div><a href="#" class="upload_new_file_link icon-add" style="padding-top:2px;">{{#t}}Upload a new file{{/t}}</a></div>
      <%= form_for :attachment, :url => context_url(@context, :context_files_url), :html => {:id => "sidebar_upload_file_form", :style => "display: none;", :multipart => true, :class => "sidebar_upload_file_form"} do |f| %>
        <input type="hidden" name="return_to" value="<%= request.url %>"/>
        <%= f.file_field :uploaded_data, :class => "file_name", :size => 20 %>
        <% if !@wiki_sidebar_data[:root_folders].empty? %>
        <div>
          <div>{{#t}}file_size_limit', %{limit %{size} per file}, :size => '50MB{{/t}}</div>
          <label for="attachment_folder_id">{{#t}}Folder{{/t}}</label>
          <select name="attachment[folder_id]" id="attachment_folder_id" class="attachment_folder_id">
            <% @wiki_sidebar_data[:root_folders].each do |folder| %>
              <option value="<%= folder.id %>"><%= folder.name %></option>
            <% end %>
          </select>
          <div id="usage-rights" data-usage-rights-required="<%= @context.feature_enabled?(:usage_rights_required) %>"></div>
        </div>
        <% end %>
        <div class="uploading" style="display: none; text-align: center;">
          <%= image_tag "ajax-loader-bar.gif", :alt => t('images.alt.loading', "Loading...") %>
        </div>
        <div class="button-container">
          <button class="btn uploadFileBtn" type="submit">{{#t}}Upload{{/t}}</button>
        </div>
        <a href="<%= context_url(@context, :context_files_url, :format => :json) %>" class="json_upload_url" style="display: none;">&nbsp;</a>
        <a href="<%= context_url(@context, :context_file_download_url, "{{ id }}", :wrap => 1) %>" class="file_url" style="display: none;">&nbsp;</a>
      <% end %>
    <% end %>
  </div>
  <% end %>
  <div aria-atomic="true" id="editor_tabs_4">
    {{#t}}Click any image to embed the image in the page.{{/t}}
    <% if can_do(@context, @current_user, :manage_files) %>
      <a href="#" aria-label="{{#t}}Click to toggle the upload a new image form (collapsed){{/t}}" class="upload_new_image_link icon-add" style="padding-top:2px;">{{#t}}Upload a new image{{/t}}</a>
    <% end %>
    <% if can_do(@context, @current_user, :manage_files) %>
      <%= form_for :attachment, :url => context_url(@context, :context_files_url), :html => {:id => "sidebar_upload_image_form", :style => "display: none;", :multipart => true} do |f| %>
        <input type="hidden" name="return_to" value="<%= request.url %>"/>
        <div style="margin-left: 10px;">
          <label for="image_folder_id">{{#t}}Folder{{/t}}</label>
          <select name="attachment[folder_id]" id="image_folder_id" aria-label="{{#t}}Select a folder to upload your image into{{/t}}">
            <% @wiki_sidebar_data[:root_folders].each do |folder| %>
              <option value="<%= folder.id %>"><%= folder.name %></option>
            <% end %>
          </select>

          <%= f.file_field :uploaded_data, :class => "file_name", :size => 20, :'aria-label' => t("aria.file_label", "Select a file, Upload limit 50") %>
          <label style="font-size: 11px" for="attachment_uploaded_data">{{#t}}file_size_limit', %{limit %{size} per file}, :size => '50MB{{/t}}</label>
          <div class="uploading" style="display: none; text-align: center;">
            <%= image_tag "ajax-loader-bar.gif", :alt => t('images.alt.loading', "Loading...") %>
          </div>
        </div>

        <div class="button-container">
          <button class="btn " type="submit">{{#t}}Upload{{/t}}</button>
        </div>
        <a href="<%= context_url(@context, :context_files_url, :format => :json) %>" class="json_upload_url" style="display: none;">&nbsp;</a>
        <a href="<%= context_url(@context, :context_file_preview_url, "{{ id }}") %>" class="file_url" style="display: none;">&nbsp;</a>
      <% end %>
    <% end %>
    <div>
      <a tabindex="0" href="#" class="find_new_image_link find">{{#t}}Find images on flickr creative commons{{/t}}</a>
    </div>
    <div id="wiki_sidebar_image_uploads" class="uploading_file_list uploading_image_list" style="display: none;">
      <div class="uploading">
        {{#t}}uploading_file', %{Uploading *file*...}, :wrapper => '<span class="filename">\1</span>{{/t}}
        <div>
          <%= image_tag "ajax-loader-bar.gif", :alt => t('images.alt.loading', "Loading...") %>
        </div>
      </div>
      <div class="remaining">
        <%= t 'files_remaining', '*0* more to upload', :wrapper => '<span class="files_remaining">\1</span>' %>
      </div>
      {{> views/shared/wiki_image}}
    </div>
    <div class="image_list_holder <%= 'file_drop' if can_do(@context, @current_user, :manage_files) %>">
      <div class="dragging_message">{{#t}}Drop Images Here to upload{{/t}}</div>
      <div class="image_list" style="max-height: 300px; overflow: auto;" data-url="<%= context_url(@context, :context_images_url) %>">
      </div>
    </div>
  </div>
  <div style="display: none;">
    <a href="<%= services_path(:service_type => "BookmarkService") %>" class="bookmark_services_url">&nbsp;</a>
    <a href="<%= bookmark_search_path(:service_type => "{{ service_type }}", :q => "{{ query }}") %>" class="bookmark_search_url">&nbsp;</a>
    <span class="context_code"><%= @context.asset_string %></span>
    <div id="wiki_sidebar_select_folder_dialog">
      <%= t 'select_destination', 'Select a destination folder for these %{count} files:', :count => '<span class="file_count">&nbsp;</span>'.html_safe %>
      <div style="margin: 20px;">
        <select class="folder_id">
          <% @wiki_sidebar_data[:root_folders].each do |folder| %>
            <option value="<%= folder.id %>"><%= folder.name %></option>
          <% end %>
        </select>
      </div>
      <div class="button-container">
        <button type="button" class="btn select_button">{{#t}}Upload Files to Folder{{/t}}</button>
        <button type="button" class="btn cancel_button btn button-secondary">{{#t}}Cancel{{/t}}</button>
      </div>
    </div>
  </div>
</div>
<% end %>
